﻿using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Navigation;

namespace SP.FlexibleStructureDeployment.Test.MyShop.Features.StructureSite
{
    [Guid("ae316304-05ee-40f4-86c3-c3aa3c2d4ae7")]
    public class StructureSiteEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite featureSite = properties.Feature.Parent as SPSite;

            CreateWebIfNeeded(featureSite, "deals", "Сделки");
            CreateWebIfNeeded(featureSite, "tasks", "Задачи");
        }

        private static void CreateWebIfNeeded(SPSite site, string strUrl, string title)
        {
            SPWeb web = site.OpenWeb(strUrl);
            if (!web.Exists)
            {
                web = site.AllWebs.Add(strUrl, title, String.Empty, site.RootWeb.Language, "STS#1", false, false);
                web.Navigation.UseShared = true;
                SPNavigationNode navigationNode = new SPNavigationNode(title, String.Format("{0}/", strUrl));
                site.RootWeb.Navigation.TopNavigationBar.AddAsLast(navigationNode);
            }
        }
    }
}
